<?php
namespace app\common\model;
use think\Model;
use think\Image;
class Img extends Model{
    public function down_load($url,$sid='vod',$pyname='',$thumb=''){
		if(is_array($url)){
			$pathArr = [];
            foreach ($url as $key => $value) {	
               if (config('upload.upload_http') && (substr($value, 0, 2) == '//' || substr($value,0,4)=='http') || config('upload.upload_http_news') && $sid=='news' && (substr($value, 0, 2) == '//' || substr($value,0,4)=='http')) {
					$down_img=$this->down_img($value,$sid,$pyname,$thumb);
						$pathArr[]=$down_img['file'];
                }else{
                        $pathArr[]=$value;
                }
			}
            return $pathArr;
		}else{
            if(config('upload.upload_http') && (substr($url,0,4)=='http' || substr($url, 0, 2) == '//')){
                return $this->down_img($url,$sid,$pyname,$thumb);
            }else{
				return ['code'=>2,'msg'=>'图片地址非远程图片','file'=>$url];
            }	
		}
		
    }	
	// 图片下载
    public function down_img($url,$sid='vod',$pyname='',$thumb=0){
        if(pic_graph_domain($url)){
			return ['code'=>3,'msg'=>'图片地址在过滤列表中','file'=>$url];
		}
		if(substr($url, 0, 2) == '//'){
			$url='http:'.$url;
		}		
		$img_type = 'jpg,jpeg,png,gif,bmp';
        $chr = strrchr($url,'.');
        if(strpos($img_type,$ext)===false){
            $chr = '.jpg';
        }
	    if(!empty($pyname) && ($sid=='vod' || $sid=='star' || $sid=='tv')){
	        $imgUrl = $pyname.$chr;    
		}else{
	        $imgUrl = md5(uniqid()).$chr;   
		}		
		$file_img = zanpian_curl_get($url,10);
		if($file_img){
	        $imgPath = $sid.'/'.date(config('upload.upload_style'),time()).'/';	
	        $filename = config('upload.upload_path').'/'.$imgPath.$imgUrl;
            $img_file=zanpian_write_file($filename,$file_img);			
			if(!$img_file){
                return ['code'=>-2,'msg'=>'图片写入失败','file'=>'error'.$url];
            }
			$data = [
			'type'  => $chr,
			'flag'  => $sid,
            'file'  => URL_PATH.$filename,
			'path'  => './'.$filename,
            'size'  => $file_size,
			'time'  => time(),
            ];
			$data['thumb'] = [];
            //生成缩略图添加水印
            if (config('upload.upload_thumb') == 1 && $thumb==1){
                $new_data=$this->makethumb($data);
                if(is_array($new_data)){
                        $data = array_merge($data,$new_data);
                }				
            }
            //添加水印
            if (config('upload.upload_water') == 1){
                $this->watermark($filename);			   
            }			
            if(config('upload.upload_type') > 0){
                $api = model('upload')->api($data['file'],$data['path']);
				if(!empty($api)){
					if(!empty($api['msg'])){
						@unlink($data['path']);
					    return ['code'=>-1,'msg'=>$api['msg'],'file'=>'error'.$url];	
					}else{
						$data['code']=1;
				        $data['msg']='上传到远程成功';
					    $data['file']=$api;
					}
				}
                if(!empty($data['thumb'])){
				    foreach($data['thumb'] as $k => $v){
					    $thumbapi= model('upload')->api($v['file'],$v['path']);
				        if(!empty($api)){
					        if(!empty($api['msg'])){
					           $data['thumb'][$k]['file']=$v['file'];						   
					        }else{
						       $data['thumb'][$k]['file']=$thumbapi;
					        }
				        }
                        unset($data['thumb'][$k]['path']);						
				    }
                }				
            }else{
				$data['code']=1;
				$data['msg']='下载成功';
				$data['file']=$data['file'];
			}				
		}else{
			$data['code']=-1;
			$data['msg']='下载失败，远程图片存在问题';
			$data['file']='error'.$url;
		}
		unset($data['path']);	
        return $data;
    }	
    public function watermark($file_path){
		$upload=config('upload');
        if(empty($upload['watermark_font'])){
            $upload['watermark_font'] = ROOT_PATH.'public/tpl/zanpianadmin/font/simhei.ttf';	
        }
        try{
            $image = Image::open($file_path);
            $image->text($upload['upload_water_txt'], $upload['watermark_font'],$upload['upload_water_size'],$upload['upload_water_color'],$upload['upload_water_pos'])->save($file_path);
        }
        catch(\Exception $e){

        }
    }
    public function makethumb($array,$new=1){
		$upload=config('upload');
        if(empty($upload['watermark_font'])){
            $upload['watermark_font'] = ROOT_PATH.'public/tpl/zanpianadmin/font/simhei.ttf';	
        }		
		if (!empty($upload['upload_thumb_size'])) {
		    try{
                $thumbs = explode(',', $upload['upload_thumb_size']);
				foreach($thumbs as $k => $v){
					
					$image = Image::open($array['path']);
                    $t_size = explode('x', strtolower($v));
                    if (!isset($t_size[1])) {
                        $t_size[1] = $t_size[0];
                    }
                    $new_thumb = $array['file'] . '_' . $t_size[0] . 'x' . $t_size[1] . '.' . strtolower(pathinfo($array['path'], PATHINFO_EXTENSION));
					$new_path = $array['path'] . '_' . $t_size[0] . 'x' . $t_size[1] . '.' . strtolower(pathinfo($array['path'], PATHINFO_EXTENSION));
                    if($new==0){
                        $new_thumb = $file_path;
                    }					
		            $image->thumb($t_size[0],$t_size[1],$upload['upload_thumb_pos'])->save($new_path); 
                    $thumb_size = round(filesize($new_path) / 1024, 2);
                    $data['thumb'][$k]['type'] = 'image';
                    $data['thumb'][$k]['flag'] = $array['flag'];
                    $data['thumb'][$k]['file'] = $new_thumb;
					$data['thumb'][$k]['path'] = $new_path;
                    $data['thumb'][$k]['size'] = $thumb_size;
                    $data['thumb'][$k]['time'] = time();						
                    if ($upload['upload_water'] == 1){// 开启文字水印
                        $image =Image::open($new_path);
                        $image->text($upload['upload_water_txt'], $upload['watermark_font'],$upload['upload_water_size'],$upload['upload_water_color'],$upload['upload_water_pos'])->save($new_path);
				   }				
				}
            }
            catch(\Exception $e){
				
            }		
		}
        return $data;
    }	
}
// +----------------------------------------------------------------------
// | ZanPianCMS
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.zanpiancms.com All rights reserved.
// +----------------------------------------------------------------------
// | BBS:  <http://www.zanpiancms.cc>
// +----------------------------------------------------------------------
?>